/*
Copyright 2005-2007 Netvibes Ltd
All rights reserved.
Author : François HODIERNE
*/

if (typeof Netvibes == "undefined" || !Netvibes) var Netvibes = {};

if (typeof Netvibes.UI == "undefined" || !Netvibes.UI) Netvibes.UI = {};

Netvibes.UI.PrefsForm = function(params) {

    /* Property: module

    *Module*: the module using the current Preference form.
    */
    this.module = params.module;
  
    /* Property: displayButton

    *Type*: ..
    */
    this.displayButton = params.displayButton;

}

Netvibes.UI.PrefsForm.prototype = {
   
  /* Property: controls

  *Collection{String->Function}*: associates the name of a control with the method creating the control.

  Those controls are used to give a "type" to the form widgets.

  Available controls are:
  * default: generates a standard text input,
  * boolean: generates a checkbox,
  * password: generates a password input,
  * textarea: generates a text area,
  * range: generates a dropdown list.
  
  */ 
  controls: {

    'default': function(pref) {
      var value = this.module.getValue(pref.name);
      if(!value || value == 'null') value = ''; // why string null ?
      var input = this.module.createElement('input');
      input.type = "text";
      input.value = value;
      input.id = 'm_' + this.module.id + '_' + pref.name;
      input.name = pref.name;
      return input;
    },
    'boolean': function(pref) {
      var input = this.module.createElement('input');
      input.type = "checkbox";
      input.id = 'm_' + this.module.id + '_' + pref.name;
      input.name = pref.name;
      if(this.module.getBool(pref.name) === true) {
        input.setAttribute('checked', 'checked');
        input.defaultChecked = true; // for IE
      }
      if(pref.onchange) {
        input.onclick = ( function(e) {
          var sender = (e.target || e.srcElement);
          if(sender.checked == true) this.module.setValue(pref.name, 'true');
          else this.module.setValue(pref.name, 'false');
          this.module.callback(pref.onchange);
        } ).bindAsEventListener(this);
      }
      return input;
    },
    'password': function(pref) {
      var input = '<input type="password" value="" id="m_' + this.module.id + '_pass" name="pass" /> ';
      return input;
    },
    'textarea': function(pref) {
      if(this.module.getValue(pref.name)) var value = this.module.getValue(pref.name); else var value = '';
      var textarea = '<textarea id="m_' + this.module.id + '_' + name + '" name="' + pref.name + '">' + value + '</textarea> ';
      return textarea;
    },
    'range': function(pref) {
      var select = this.module.createElement('select');
      select.id = 'm_' + this.module.id + '_' + pref.name;
      select.name = pref.name;
      if (parseInt(pref.step) > 0) {
	      for(var i=parseInt(pref.min); i<=parseInt(pref.max); i+=parseInt(pref.step)) {
	        var option = this.module.createElement('option');
	        if(this.module.getValue(pref.name) == i) option.setAttribute('selected', 'selected');
	        option.value = i; option.setText(i);
	        select.appendChild(option);
	      }
      }
      if(pref.onchange) {
        select.onchange = ( function(e) {
          var sender = (e.target || e.srcElement);
          this.module.setValue(pref.name, sender.value)
          this.module.callback(pref.onchange);
        } ).bindAsEventListener(this);
      }
      return select;
    },
    'list': function(pref) {
      var select = this.module.createElement('select');
      select.id = 'm_' + this.module.id + '_' + pref.name;
      select.name = pref.name;
      for(var i=0; i<pref.options.length; i++) {
        var option = this.module.createElement('option');
        if(this.module.getValue(pref.name) == pref.options[i].value) option.setAttribute('selected', 'selected');
        option.value = pref.options[i].value;
        option.setText( (pref.options[i].label ? pref.options[i].label : pref.options[i].value ) );
        select.appendChild(option);
      }
      if(pref.onchange) {
        select.onchange = ( function(e) {
          var sender = (e.target || e.srcElement);
          this.module.setValue(pref.name, sender.value)
          this.module.callback(pref.onchange);
        } ).bindAsEventListener(this);
      }
      return select;
    },
    'color': function(pref) {
      if(typeof pref.colors == "undefined")
        pref.colors = ['white', 'yellow', 'green', 'red', 'blue', 'orange'];
      var colorSelector = this.module.createElement('div');
      colorSelector.addClassName('postItcolorSelection');
      for(i=0; i<pref.colors.length;i++) {
        var color = this.module.createElement('div');
        //color.addClassName(colors[i]);
        color.setStyle('background-color', pref.colors[i]);
        color.onclick = ( function(e) {
          var sender = (e.target || e.srcElement);
          this.module.setValue(pref.name, sender.style.backgroundColor);
          if(pref.onchange) this.module.callback(pref.onchange);
        } ).bindAsEventListener(this);
        colorSelector.appendChild(color);
      };
      return colorSelector;
    }
  
  },

  /* Method: getContent

  Gets the content of the preference form, for display.

  Parameters:
  * None.
  
  Returns:
  * String: the HTML version of the preference form.
  */
  getContent: function() {
    
    var editContent = '';
    
    this.form = this.module.createElement("form");
     
    var table = this.module.createElement("table");
    table.addClassName("formTable");
    table.setStyle('width', '100%');
    
    var tbody = this.module.createElement("tbody");
    
    for(var i = 0; i < this.module.preferences.length; i++) {
      
      var tr = this.module.createElement("tr");
      var pref = this.module.preferences[i];
      
      if(pref.type == 'hidden') continue;
      
      if(!pref.label) pref.label = pref.name;
      var label = this.module.createElement("label");
      label.setAttribute('for', 'm_' + this.module.id + '_' + pref.name);
      label.setHTML( _(pref.label+':') );
      var tdl = this.module.createElement("td");
      tdl.appendChild(label);
      tr.appendChild(tdl);
      
      if(!this.controls[pref.type]) pref.type = 'default';
      var control = this.controls[pref.type].bind(this)(pref);
      var tdc = this.module.createElement("td");
      
      tdc.setContent(control);
      tr.appendChild(tdc);
      
      tbody.appendChild(tr);
      
    }
    
    if(this.displayButton == undefined || this.displayButton === true) {
      var submit = this.module.createElement('input');
      submit.type = 'submit';
      submit.value = _("Ok");
      submit.addClassName('buttonClean');
      var tr = this.module.createElement("tr");
      var tds = this.module.createElement("td");
      tds.colSpan = '2';
      tds.appendChild(submit);
      tr.appendChild(tds);
      tbody.appendChild(tr);
    }
    
    table.appendChild(tbody);
    
    this.form.appendChild(table);
    
    this.form.onsubmit = ( function() {
      var callback = ( function() { this.callback('endEdit') } ).bind(this.module);
      this.saveValues();
      // force to saveValues immediatly and add a callback when it's done
      this.module.saveValues(callback);
      return false;
    } ).bindAsEventListener(this);
    
    return this.form;
    
  },
  
  /* to document */
  saveValues: function(form) {
    
    if(typeof form == 'undefined') var form = this.form;

    var formElements = UWA.Form.getElements(form);

    for (var j = 0; j < formElements.length; j++) {
      var el = formElements[j];
      if(el.type == "submit") continue;
      switch(el.type) {
        case 'password':
          if(el.value != '' && el.value != 'XXNVXX' )
            this.module.setValue(el.name, el.value)
          break;
        case 'checkbox':
          if(el.checked) this.module.setValue(el.name, 'true');
          else this.module.setValue(el.name, 'false');
          break;  
        case 'radio':
          if(el.checked)
            this.module.setValue(el.name, el.value);
          break;
        default :
          this.module.setValue(el.name, el.value);
        break;
      }
    }
    
  }

}